import re

class ScriptLine:
    def __init__(self, index, text, processor):
        self.processor = processor # 父级的处理器对象
        self.index = index          # 行号
        self.text = text.strip()    # 行内容（自动去除首尾空白）
         # 去掉|，这个东西是管支线的，我就全部显示就行了。
        if self.text.startswith("|"):
            self.text = self.text[1:]
        self.is_discard = False     # 是否废弃
        self.xml = None             # 处理后的XML文本
        self.attr = ""              # 用于处理属性

    def __repr__(self):
        return f"Line {self.index}: {'[X]' if self.is_discard else '[ ]'} {self.text[:20]}..."

    def discard(self):
        self.is_discard = True
        return True # 返回True表示此项已经成功处理

    def set_command_xml(self, command):
        self.save("Command", command)
        return True

    def is_prefix(self, text):
        return self.text.startswith(text)

    def contain(self, text):
        return text in self.text

    def has_text(self, text):
        return text in self.text

    def replace_all_text(self, text):
        """
        根据替换列表替换文本中的所有指定字符串
        """
        replace_list = [
            ["\\.", "。"],
            ["\\!", "！"],
            ["\\i[0]", ""],
            ["\\i", ""],
            ["\\E", ""]
        ]

        for pattern, replacement in replace_list:
            # 使用正则表达式进行替换
            text = text.replace(pattern, replacement)

        return text

    def add_not_default(self, result, name, default_value):
        if result[name] == default_value:
            return
        self.add_attr(name, result[name])

    def add_attr_test(self, name, value, default_value):
        if value == default_value:
            return
        self.add_attr(name, value)

    def add_attr(self, name, value):
        if value:
          self.attr += f"{name}='{value}' "

    def find_int(self, pattern, default_value = 0):
        match = re.search(pattern, self.text)
        if match:
            return int(match.group(1))
        return default_value

    def find_int2(self, pattern):
        match = re.search(pattern, self.text)
        a = 0
        b = 0
        if match:
            a, b = map(int, match.groups())
        return a, b

    def find_int3(self, pattern):
        match = re.search(pattern, self.text)
        a = 0
        b = 0
        c = 0
        if match:
            a, b, c = map(int, match.groups())
        return a, b, c

    def find_str(self, pattern):
        match = re.search(pattern, self.text)
        if match:
            return str(match.group(1))
        return None

    def find_quote_str(self, name):
      return self.find_str(f'{name}="([^"]*)"')

    def find_equal_str(self, name):
      return self.find_str(f'{name}=([^"]*)')

    def find_quote_int(self, name):
      return self.find_int(f'{name}="(\d+)"')

    def find_equal_int(self, name):
      return self.find_int(f'{name}=(\d+)')

    def save(self, type, content = "", extra_attr = ""):
        if content is None: content = ""
        if extra_attr is None: extra_attr = ""
        self.xml = f'<Item Type="{type}" {self.attr}{extra_attr}>{content}</Item>'
        return True

    # 给这个项目增加一条额外的语句
    def add_extra_line(self, line):
        self.xml += f"\n{line}"

    def save_undefined(self):
        self.xml = f'<Item>{self.text}</Item>'